package com.fypg.module.medical.service.outpatientorderdetail;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.fypg.module.medical.controller.admin.outpatientorderdetail.vo.*;
import com.fypg.module.medical.dal.dataobject.outpatientorderdetail.OutpatientOrderDetailDO;
import com.fypg.framework.common.pojo.PageResult;
import com.fypg.framework.common.pojo.PageParam;
import com.fypg.framework.common.util.object.BeanUtils;

import com.fypg.module.medical.dal.mysql.outpatientorderdetail.OutpatientOrderDetailMapper;

import static com.fypg.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.fypg.framework.common.util.collection.CollectionUtils.convertList;
import static com.fypg.framework.common.util.collection.CollectionUtils.diffList;
import static com.fypg.module.medical.enums.ErrorCodeConstants.*;

/**
 * 门诊订单详情 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OutpatientOrderDetailServiceImpl implements OutpatientOrderDetailService {

    @Resource
    private OutpatientOrderDetailMapper outpatientOrderDetailMapper;

    @Override
    public String createOutpatientOrderDetail(OutpatientOrderDetailSaveReqVO createReqVO) {
        // 插入
        OutpatientOrderDetailDO outpatientOrderDetail = BeanUtils.toBean(createReqVO, OutpatientOrderDetailDO.class);
        outpatientOrderDetailMapper.insert(outpatientOrderDetail);
        // 返回
        return outpatientOrderDetail.getId();
    }

    @Override
    public void updateOutpatientOrderDetail(OutpatientOrderDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateOutpatientOrderDetailExists(updateReqVO.getId());
        // 更新
        OutpatientOrderDetailDO updateObj = BeanUtils.toBean(updateReqVO, OutpatientOrderDetailDO.class);
        outpatientOrderDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteOutpatientOrderDetail(String id) {
        // 校验存在
        validateOutpatientOrderDetailExists(id);
        // 删除
        outpatientOrderDetailMapper.deleteById(id);
    }

    @Override
        public void deleteOutpatientOrderDetailListByIds(List<String> ids) {
        // 校验存在
        validateOutpatientOrderDetailExists(ids);
        // 删除
        outpatientOrderDetailMapper.deleteByIds(ids);
        }

    private void validateOutpatientOrderDetailExists(List<String> ids) {
        List<OutpatientOrderDetailDO> list = outpatientOrderDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(OUTPATIENT_ORDER_DETAIL_NOT_EXISTS);
        }
    }

    private void validateOutpatientOrderDetailExists(String id) {
        if (outpatientOrderDetailMapper.selectById(id) == null) {
            throw exception(OUTPATIENT_ORDER_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public OutpatientOrderDetailDO getOutpatientOrderDetail(String id) {
        return outpatientOrderDetailMapper.selectById(id);
    }

    @Override
    public PageResult<OutpatientOrderDetailDO> getOutpatientOrderDetailPage(OutpatientOrderDetailPageReqVO pageReqVO) {
        return outpatientOrderDetailMapper.selectPage(pageReqVO);
    }

}